﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class private_WebServices_getListingThumb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // get the file name -- fall800.jpg
        string file = Request.QueryString["file"];
        if(file.Contains("~"))
            file = ResolveUrl(file);
        int targetWidth = Convert.ToInt32(Request.QueryString["w"]);
        int targetHeight = Convert.ToInt32(Request.QueryString["h"]);

        if (!File.Exists(Server.MapPath(file)))
        {
            Response.Clear();
            Response.StatusCode = 404;
            Response.End();
            return;
        }

        // create an image object, using the filename we just retrieved
        System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file));
        int finalWidth = 0, finalHeight = 0;

        if (image.Width > image.Height)
        {
            finalWidth = image.Width;
            if (finalWidth > targetWidth) finalWidth = targetWidth;
            finalHeight = GetProportionalHeight(image.Width, image.Height, finalWidth);
        }
        else
        {
            finalHeight = image.Height;
            if (finalHeight > targetHeight) finalHeight = targetHeight;
            finalWidth = GetProportionalWidth(image.Width, image.Height, finalHeight);
        }

        // create the actual thumbnail image
        // System.Drawing.Image thumbnailImage = image.GetThumbnailImage(finalWidth, finalHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

        System.Drawing.Image thumbnailImage = new System.Drawing.Bitmap(finalWidth, finalHeight);
        System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(thumbnailImage);

        graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

        graphic.DrawImage(image, 0, 0, finalWidth, finalHeight);

        // make a memory stream to work with the image bytes
        MemoryStream imageStream = new MemoryStream();

        // put the image into the memory stream
        thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        // make byte array the same size as the image
        byte[] imageContent = new Byte[imageStream.Length];

        // rewind the memory stream
        imageStream.Position = 0;

        // load the byte array with the image
        imageStream.Read(imageContent, 0, (int)imageStream.Length);

        // return byte array to caller with image type
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(imageContent);
    }

    ///  <summary>
    /// Required, but not used
    /// </summary>
    /// <returns>true</returns>
    public bool ThumbnailCallback()
    {
        return true;
    }

    private int GetProportionalHeight(int parentWidth, int parentHeight, int targetWidth)
    {
        int height = 0;
        try
        {
            height = (int)(parentHeight * ((double)targetWidth / (double)parentWidth));
        }
        catch (Exception) { }
        return height;
    }

    private int GetProportionalWidth(int parentWidth, int parentHeight, int targetHeight)
    {
        int width = 0;
        try
        {
            width = (int)(parentWidth * ((double)targetHeight / (double)parentHeight));
        }
        catch (Exception) { }
        return width;
    }
}
